package com;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/com/ServletRegister")
public class ServletRegister extends HttpServlet {
    private final static int REGISTER_ERROR=-2;
    private final static int REGISTER_SUCCESS=2;
    private final static int REGISTER=2;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String path=this.getServletContext().getRealPath("/WEB-INF/userlist.txt");
        int flag=FileUnit.register(username,password,path);
        if(flag==REGISTER_SUCCESS)
            request.getRequestDispatcher("/RegisterSuccess.jsp").forward(request,response);
        else if(flag==REGISTER_ERROR)
            request.getRequestDispatcher("/RegisterLoss.jsp").forward(request,response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
